package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //我喜欢的数据统计
    @Override
    public long counts(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        //查询我喜欢的
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    //粉丝数据统计
    @Override
    public Integer fanCounts(Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        Integer count = Math.toIntExact(mongoTemplate.count(query, UserLike.class));
        return count;
    }

    /**
     * 查询用户喜欢你得列表
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<UserLike> findLoves(Long userId, Integer page, Integer pagesize) {

        Criteria criteria = Criteria.where("likeUserId").is(userId)
                .and("isLike").is(true);
        Query query = Query.query(criteria)
                .limit(pagesize)
                .skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("updated")));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        return userLikeList;
    }


    //我喜欢的列表
    @Override
    public List<UserLike> lLove(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    //粉丝列表
    @Override
    public List<UserLike> loveMe(Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);

        return userLikes;
    }

    @Override
    public void update(Long uid, Long userId) {
        //修改like表中islike的值
        Criteria criteria = Criteria.where("likeUserId").is(uid).and("userId").is(userId);
        Query query = Query.query(criteria);
        Update update = Update.update("isLike", false);

        mongoTemplate.updateFirst(query, update, UserLike.class);

        //删除friend中的数据
        Criteria criteria1 = Criteria.where("userId").is(userId).and("friendId").is(uid);
        Query query1 = Query.query(criteria1);
        mongoTemplate.remove(query1, Friend.class);

    }

    @Override
    public void like(Long userId, Long uid) {
        Criteria criteria = Criteria.where("likeUserId").is(uid).and("userId").is(userId);
        Query query = Query.query(criteria);
        Update update = Update.update("isLike", false);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        Boolean isLike = false;
        for (UserLike userLike : userLikes) {
            isLike = userLike.getIsLike();
        }
        if (isLike == true) {
            mongoTemplate.updateFirst(query, update, UserLike.class);
            //删除friend中的数据
            Criteria criteria1 = Criteria.where("userId").is(userId).and("friendId").is(uid);
            Query query1 = Query.query(criteria1);
            mongoTemplate.remove(query1, Friend.class);
        }

        Update update1 = Update.update("isLike", true);
        mongoTemplate.updateFirst(query, update1, UserLike.class);
        List<Friend> list = mongoTemplate.find(query, Friend.class);
        if (CollUtil.isEmpty(list)) {
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(uid);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }


    }


}



